#!/bin/bash

usage() {
    cat << EOF
usage: `basename $0` [OPTIONS]
  --threads NUM         The number of threads to use for running tests.
  --multilib-arch ARCH  Use this option to test 32-bit libs/binaries on
                        64-bit hosts.
EOF
}

threads_arg=''

while [ $# -gt 0 ]; do
    case $1 in
        --threads)
            shift
            threads_arg="--threads $1"
            ;;
        --multilib-arch)
            shift
            ARCH=$1
            ;;
        * )
            echo "unknown option: $1"
            echo ""
            usage
            exit 1
            ;;
    esac
    shift
done

if [ `whoami` = "root" ]; then
    echo "error: lld tests do not support running as root."
    exit 1
fi

set -xe

if [ -z "$ARCH" ]; then
    ARCH=`rpm --eval '%_arch'`
fi

case $ARCH in
    arm)
        ;&
    i686)
        LIB_DIR="/usr/lib/"
        ;;
    *)
        LIB_DIR="/usr/lib64/"
        ;;
esac

cd $(mktemp -d -p /var/tmp)
ln -s /usr/include include
tar -xzf /usr/share/lld/src/test.tar.gz
ln -s /usr/share/lld/src/$ARCH.site.cfg.py test/lit.site.cfg.py
ln -s /usr/share/lld/src/$ARCH.Unit.site.cfg.py test/Unit/lit.site.cfg.py

LD_LIBRARY_PATH=$LIB_DIR/lld:$LD_LIBRARY_PATH \
lit -v -s $threads_arg test \
        -Dlld_obj_root=`pwd` \
        -Dlld_test_root=`pwd`/test \
        -Dlld_unittest_bindir=$LIB_DIR/lld
